/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.set;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.jetbrains.annotations.NotNull;
import redora.api.Persistable;

import java.util.Set;

public class PersistableTestImpl implements Persistable {

    protected Long id;
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public boolean isDirty(Set<Persistable> ignore) {
        return true;
    }

    @NotNull
    @Override
    public PersistableTestImpl clone(int depth) {
        return this;
    }

    @Override
    /** Checks primarily on Id, if there is no Id, it will try hashCode. */
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (!(that instanceof PersistableTestImpl)) {
            return false;
        }
        PersistableTestImpl t = (PersistableTestImpl) that;
        if ((id != null) && id.equals(t.id)) {
            return true;
        }
        return id == null && t.id == null && hashCode() == t.hashCode();
    }

    /**
     * The hashCode is computed on the Id which is as fast as it can get. If there is
     * no Id (new object), the hash code is calculated on all the other fields.
     *
     * @return the hash code of Id, or, when null, a hash code created by all the other fields.
     */
    @Override
    public int hashCode() {
        if (id != null) {
            return id.hashCode();
        }
        return new HashCodeBuilder(3, 37)
                .append(content)
                .toHashCode();
    }
}
